<?php

class CTag extends Zend_Db_Table {

	private $itemId;
	
	private $tagString;
	
	protected $_name;
	
	protected $_primary;
	
	public function __construct(){
		
		$this->_name=R_PRE."tags";
		
		$this->_primary="tagid";
		
		parent::__construct();
		
	}
	
	public function createTags($string, $itemId) {
		
		$db=$this->getAdapter();
		
		$cur_tagsArray = $this->splitTheseTags ( $string );
		
		for($i = 0; $i < count ( $cur_tagsArray ); $i ++) {
			
			$db->query ( "INSERT INTO {$this->_name}(itemid,tagname) values('$itemId','" . $cur_tagsArray [$i] . "')" );
		
		}
		
		return true;
	
	}
	
	public function editTags($string, $itemId) {
		
		$db=$this->getAdapter();
		
		$cur_tagsArray = $this->splitTheseTags ($string);
		
		$result=$db->query ( "SELECT tagname FROM {$this->_name} WHERE itemid='" . $itemId . "'" );
		
		$thisArray=$result->fetchAll ();
		
		for($i=0;$i<count($thisArray);$i++){
			
			$pre_tagsArray[]=$thisArray[$i]['tagname'];
			
		}
		
		
		$cur_counts = count ( $cur_tagsArray );
		
		$pre_counts = count ( $pre_tagsArray );
		
		$delArray = array ();
		
		try {
			
			$flag = false;
			
			for($i = 0; $i < $pre_counts; $i ++) {
				
				for($j = 0; $j < $cur_counts; $j ++) {
					
					if ($pre_tagsArray [$i] == $cur_tagsArray [$j]) {
						
						$flag = true;
						
						break;
					
					}
				
				}
				
				if ($flag == false) {
					
					array_push ( $delArray, $pre_tagsArray [$i] );
				
				}
				
				$flag = false;
			
			}
			
			$delString = join ( "','", $delArray );
			
			$delString = "'" . $delString . "'";
			
			if ($delString != "") {
				
				$db->query ( "DELETE FROM {$this->_name} WHERE itemid='" . $itemId . "' AND tagname IN ({$delString})" );
			
			}
			
			$addArray = array ();
			
			$flag = true;
			
			for($j = 0; $j < $cur_counts; $j ++) {
				
				for($i = 0; $i < $pre_counts; $i ++) {
					
					if ($cur_tagsArray [$j] == $pre_tagsArray [$i]) {
						
						$flag = false;
						
						break;
					
					}
				
				}
				
				if ($flag == true) {
					
					array_push ( $addArray, $cur_tagsArray [$j] );
				
				}
				
				$flag = true;
			
			}
			
			//print_r($addArray);
			

			if ($addArray [0] != "") {
				
				for($i = 0; $i < count ( $addArray ); $i ++) {
					
					$db->query ( "INSERT INTO {$this->_name}(itemid,tagname) values('" . $itemId . "','" . $addArray [$i] . "')" );
				
				}
			
			}
			
			return true;
		
		} 

		catch ( Exception $e ) {
			
			$this->setError ( $e->getMessage () );
		
		}
		
		return false;
	
	}
	
	public function splitTheseTags($string) {
		
		$string = trim ( $string );
		
		$thisTagsArray = array_unique ( preg_split ( "/[\\s,]/", $string ) );
		
		$thisArray = array ();
		
		foreach ( $thisTagsArray as $value ) {
			
			if (preg_match ( "/^[\\da-zA-Z\\x80-\\xff]{1,16}$/", $value )) {
				
				array_push ( $thisArray, $value );
			
			}
		
		}
		
		return $thisArray;
	
	}
	
	public function getDetails($itemid) {
		
		$db=$this->getAdapter();
		
		$result = $db->query ("SELECT * FROM $this->_name WHERE itemid='$itemid'");
		
		return $result->fetchAll();
	
	}
	
	public function getDetailsToString($itemid){
		
		$thisString="";
		
		$thisArray=$this->getDetails($itemid);
		
		for($i=0;$i<count($thisArray);$i++){
			
			$thisString.=$thisArray[$i]['tagname'].' ';
			
		}
		
		return trim($thisString);
		
	}
	
	public function delTags($itemidArray) {
		
		$db=$this->getAdapter();
		
		$itemIdString = join ( ",", $itemidArray );
		
		try {
			
			$db->query ( "DELETE FROM $this->_name WHERE itemid in ({$itemIdString})" );
			
			return true;
		
		} 

		catch ( Exception $e ) {
			
			$this->error = $e->getMessage ();
		
		}
		
		return false;
	
	}
	
	public function getTagsCounts() {
		
		$this->setDB ();
		
		$this->db->query ( "SELECT DISTINCT tagname FROM " . B_PRE . "tags" );
		
		$thisArray = $this->db->getSingleArray ();
		
		return count ( $thisArray );
	}
	
	public function getTags($from, $counts, $order) {
		
		$this->setDB ();
		
		if ($order == "hot") {
			
			$this->db->query ( "SELECT tagname FROM " . B_PRE . "tags 

							GROUP BY tagname 

							ORDER BY COUNT(tagname) DESC LIMIT {$from},{$counts}" );
		
		} 

		else if ($order == "new") {
			
			$this->db->query ( "SELECT tagname FROM " . B_PRE . "tags

							GROUP BY tagname

							ORDER BY tagid DESC LIMIT {$from},{$counts}" );
		
		}
		
		return $this->db->getSingleArray ();
	
	}
	
	public function getTagsByUidCounts($uid) {
		
		$this->setDB ();
		
		$this->db->query ( "SELECT DISTINCT tagname FROM " . B_PRE . "tags," . B_PRE . "items 

						WHERE " . B_PRE . "items.uid='$uid' 

						AND " . B_PRE . "items.itemid=" . B_PRE . "tags.itemid 

					  " );
		
		return $this->db->getSingleArray ();
	
	}
	
	public function getTagsByUid($from, $counts, $uid, $order) {
		
		$this->setDB ();
		
		if ($order == "hot") {
			
			$this->db->query ( "SELECT tagname FROM " . B_PRE . "tags," . B_PRE . "items 

							WHERE " . B_PRE . "items.uid='$uid' 

							AND " . B_PRE . "items.itemid=" . B_PRE . "tags.itemid 

							GROUP BY tagname 

							ORDER BY COUNT(tagname) DESC LIMIT {$from},{$counts}" );
		
		} 

		else if ($order == "new") {
			
			$this->db->query ( "SELECT tagname FROM " . B_PRE . "tags," . B_PRE . "items 

							WHERE " . B_PRE . "items.uid='$uid' 

							AND " . B_PRE . "items.itemid=" . B_PRE . "tags.itemid 

							GROUP BY tagname 

							ORDER BY tagid DESC LIMIT {$from},{$counts}" );
		
		}
		
		return $this->db->getSingleArray ();
	
	}
	
	public function getTagsItemsCounts($tagname) {
		
		$this->setDB ();
		
		$this->db->query ( "SELECT DISTINCT " . B_PRE . "tags.itemid 

						FROM " . B_PRE . "tags

						WHERE " . B_PRE . "tags.tagname='$tagname'" );
		
		return count ( $this->db->getSingleArray () );
	
	}
	
	public function getTagsItemsByUidCounts($tagname, $uid) {
		
		$this->setDB ();
		
		$this->db->query ( "SELECT DISTINCT " . B_PRE . "tags.itemid 

						FROM " . B_PRE . "tags," . B_PRE . "items

						WHERE " . B_PRE . "tags.itemid=" . B_PRE . "items.itemid 

						AND " . B_PRE . "items.uid='$uid' 

						AND " . B_PRE . "tags.tagname='$tagname' " );
		
		return count ( $this->db->getSingleArray () );
	
	}
	
	public function getTagsItems($from, $counts, $tagname) {
		
		$this->setDB ();
		
		$this->db->query ( "SELECT DISTINCT " . B_PRE . "tags.itemid AS tagitemid," . B_PRE . "items.* 

						FROM " . B_PRE . "tags," . B_PRE . "items

						WHERE " . B_PRE . "tags.tagname='$tagname' 

						AND " . B_PRE . "tags.itemid=" . B_PRE . "items.itemid 

						ORDER BY " . B_PRE . "items.itemid DESC LIMIT {$from},{$counts}" );
		
		return $this->db->getMulArray ( "assoc" );
	
	}
	
	public function getTagsItemsByUid($from, $counts, $tagname, $uid) {
		
		$this->setDB ();
		
		$this->db->query ( "SELECT DISTINCT " . B_PRE . "tags.itemid," . B_PRE . "items.* 

						FROM " . B_PRE . "tags," . B_PRE . "items

						WHERE " . B_PRE . "tags.itemid=" . B_PRE . "items.itemid 

						AND " . B_PRE . "items.uid='$uid' 

						AND " . B_PRE . "tags.tagname='$tagname' 

						ORDER BY " . B_PRE . "items.itemid DESC LIMIT {$from},{$counts}" );
		
		return $this->db->getMulArray ( "assoc" );
	
	}

}